package top.sdwu_top.kbase.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2Config  {
    private List<Parameter> generateHeaderWithToken() {
        ParameterBuilder builder = new ParameterBuilder();
        List<Parameter> headers = new ArrayList<>();
        Parameter accessToken = builder.name("Authorization").description("Bearer").modelRef(new ModelRef("string")).parameterType("header")
                .required(false).defaultValue("Bearer df65ad82-bc3f-4e5c-b533-2642ee3a84e5").build();
        headers.add(accessToken);
        return headers;
    }
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                // .paths(PathSelectors.regex("/api/.*"))
                // .apis(RequestHandlerSelectors.basePackage("cn.nvr.rental.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(generateHeaderWithToken()).pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("段志鹏")
                .description("山东女子学院")
                .termsOfServiceUrl("http://sdwu-top.top/")
                .contact(new Contact("zhipeng.duan", "http://sdwu-top.top/", "673270700@qq.com"))
                .version("1.0")
                .build();
    }

}